//
//  CXHViewController.m
//  CXHAdSDK-GroMore
//
//  Created by 二跳 on 10/15/2024.
//  Copyright (c) 2024 二跳. All rights reserved.
//

#import "CXHViewController.h"
#import <BUAdSDK/BUAdSDK.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHViewController ()<BUNativeAdsManagerDelegate, BUNativeAdDelegate, BUNativeExpressAdViewDelegate, BUNativeExpressBannerViewDelegate, BUNativeExpressFullscreenVideoAdDelegate>

@property (nonatomic, strong) BUNativeAdsManager *nativeAd;

@property (nonatomic, strong) BUNativeAd *ad;

@property (nonatomic, strong) BUNativeAdMediation *mediation;

@property (nonatomic, strong) BUNativeExpressBannerView *bannerView;

@property (nonatomic, strong) BUNativeExpressFullscreenVideoAd *interstitialAd;

@end

@implementation CXHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self loadNative:YES];
        [self loadAndShowBanner];
//        [self loadInter];
    });
    
}

#pragma mark - 插屏
- (void)loadInter {
    self.interstitialAd = [[BUNativeExpressFullscreenVideoAd alloc]initWithSlotID:@"103188365"];
    self.interstitialAd.delegate = self;
    [self.interstitialAd loadAdData];
}

- (void)showInter{
    [self.interstitialAd showAdFromRootViewController:self];
}

#pragma mark - BUNativeExpressFullscreenVideoAdDelegate
/**
 This method is called when video ad material loaded successfully.
 */
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    [self showInter];
}

/**
 This method is called when video ad materia failed to load.
 @param error : the reason of error
 */
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error{
    
}

/**
 This method is called when rendering a nativeExpressAdView successed.
 It will happen when ad is show.
 */
// Mediation:/// @Note :  (针对聚合维度广告)<6300版本不会回调该方法，>=6300开始会回调该方法，但不代表最终展示广告的渲染结果。
- (void)nativeExpressFullscreenVideoAdViewRenderSuccess:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd{
    
}

/**
 This method is called when a nativeExpressAdView failed to render.
 @param error : the reason of error
 */
// Mediation:/// @Note :  (针对聚合维度广告)<6300版本不会回调该方法，>=6300开始会回调该方法，但不代表最终展示广告的渲染结果。
- (void)nativeExpressFullscreenVideoAdViewRenderFail:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error{
    
}

/**
 This method is called when video cached successfully.
 For a better user experience, it is recommended to display video ads at this time.
 And you can call [BUNativeExpressFullscreenVideoAd showAdFromRootViewController:].
 */
- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when video ad slot will be showing.
 */
// Mediation:/// @Note :  当加载聚合维度广告时，支持该回调的adn有：CSJ
- (void)nativeExpressFullscreenVideoAdWillVisible:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when video ad slot has been shown.
 */
- (void)nativeExpressFullscreenVideoAdDidVisible:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when video ad is clicked.
 */
- (void)nativeExpressFullscreenVideoAdDidClick:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when the user clicked skip button.
 */
- (void)nativeExpressFullscreenVideoAdDidClickSkip:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when video ad is about to close.
 */
// Mediation:/// @Note :  当加载聚合维度广告时，支持该回调的adn有：CSJ
- (void)nativeExpressFullscreenVideoAdWillClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when video ad is closed.
 */
- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}

/**
 This method is called when video ad play completed or an error occurred.
 @param error : the reason of error
 */
- (void)nativeExpressFullscreenVideoAdDidPlayFinish:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error{
    
}

/**
This method is used to get the type of nativeExpressFullScreenVideo ad
 */
// Mediation:/// @Note :  当加载聚合维度广告时，不支持该回调。
- (void)nativeExpressFullscreenVideoAdCallback:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd withType:(BUNativeExpressFullScreenAdType) nativeExpressVideoAdType{
    
}

/**
 This method is called when another controller has been closed.
 @param interactionType : open appstore in app or open the webpage or view video ad details page.
 */
- (void)nativeExpressFullscreenVideoAdDidCloseOtherController:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd interactionType:(BUInteractionType)interactionType{
    
}

#pragma mark - banner
- (void)loadAndShowBanner {
    self.bannerView = [[BUNativeExpressBannerView alloc]initWithSlotID:@"103329851" rootViewController:self adSize:CGSizeMake(kCXHScreenWidth, kCXHScreenWidth * 5 / 32)];
    self.bannerView.frame = CGRectMake(0, 100, kCXHScreenWidth, kCXHScreenWidth * 5 / 32);
    self.bannerView.delegate = self;
    [self.bannerView loadAdData];
    [self.view addSubview:self.bannerView];
}
#pragma mark - BUNativeExpressBannerViewDelegate
/**
 This method is called when bannerAdView ad slot loaded successfully.
 @param bannerAdView : view for bannerAdView
 */
- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView{
    
}

/**
 This method is called when bannerAdView ad slot failed to load.
 @param error : the reason of error
 */
- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *_Nullable)error{
    
}

/**
 This method is called when rendering a nativeExpressAdView successed.
 */
// Mediation:/// @Note :  (针对聚合维度广告)<6400版本不会回调该方法，>=6400开始会回调该方法，但不代表最终展示广告的渲染结果。
- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView{
    
}

/**
 This method is called when a nativeExpressAdView failed to render.
 @param error : the reason of error
 */
// Mediation:/// @Note :  (针对聚合维度广告)<6400版本不会回调该方法，>=6400开始会回调该方法，但不代表最终展示广告的渲染结果。
- (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError * __nullable)error{
    
}

/**
 This method is called when bannerAdView ad slot showed new ad.
// Mediation:@Note :  Mediation dimension does not support this callBack.
 */
- (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView{
    
}

/**
 This method is called when bannerAdView is clicked.
 */
- (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView{
    
}

/**
 This method is called when the user clicked dislike button and chose dislike reasons.
 @param filterwords : the array of reasons for dislike.
 */
- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray<BUDislikeWords *> *_Nullable)filterwords{
    
}

/**
 This method is called when another controller has been closed.
 @param interactionType : open appstore in app or open the webpage or view video ad details page.
 */
- (void)nativeExpressBannerAdViewDidCloseOtherController:(BUNativeExpressBannerView *)bannerAdView interactionType:(BUInteractionType)interactionType{
    
}

/**
 This method is called when the Ad view container is forced to be removed.
 @param bannerAdView : Express Banner Ad view container
 */
- (void)nativeExpressBannerAdViewDidRemoved:(BUNativeExpressBannerView *)bannerAdView{
    
}
#pragma mark - native

- (void)loadNative:(BOOL)isExpressAd {
    BUAdSlot *slot1 = [[BUAdSlot alloc] init];
    slot1.ID = @"103175055";
    if (isExpressAd) {
        slot1.ID = @"103174518";
    }
    slot1.AdType = BUAdSlotAdTypeFeed;
    BUSize *imgSize = [BUSize sizeBy:BUProposalSize_Feed690_388];
    slot1.imgSize = imgSize;
    slot1.adSize = CGSizeMake(kCXHScreenWidth, 10);
    slot1.position = BUAdSlotPositionFeed;
    
    
    _nativeAd = [[BUNativeAdsManager alloc] initWithSlot:slot1];
    _nativeAd.delegate = self;
    _nativeAd.mediation.rootViewController = self;
//    _nativeAd.mediation.
    _nativeAd.nativeExpressAdViewDelegate = self;
    [self.nativeAd loadAdDataWithCount:1];
}
#pragma mark - BUNativeAdsManagerDelegate
- (void)nativeAdsManagerSuccessToLoad:(BUNativeAdsManager *)adsManager nativeAds:(NSArray<BUNativeAd *> *_Nullable)nativeAdDataArray{
    BUNativeAd *ad = nativeAdDataArray.firstObject;
    ad.delegate = self;
    ad.rootViewController = self;
    _ad = ad;
    
    
    if (ad.mediation.isExpressAd) {
        BUMCanvasView *canvasView = ad.mediation.canvasView;
        CGFloat h = canvasView.frame.size.height;
        canvasView.frame = CGRectMake(0, 100, kCXHScreenWidth, h);
        [self.view addSubview:canvasView];
        
        [ad.mediation render];
        return;
    }
    UIView *adView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, kCXHScreenWidth, kCXHScreenWidth * 9 / 16 + 30)];
    
    NSLog(@"======%d", ad.mediation.isExpressAd);
    BUMCanvasView *canvasView = ad.mediation.canvasView;
    canvasView.frame = adView.bounds;
    [adView addSubview:canvasView];
    
    UIImageView *imageView = ad.mediation.canvasView.imageView;
    imageView.frame = CGRectMake(16, 10, kCXHScreenWidth - 32, (kCXHScreenWidth - 32) * 9 / 16);
    [adView addSubview:imageView];
    
    CGFloat h = 10 + (kCXHScreenWidth - 16) * 9 / 16;
    UIView *logoView = ad.mediation.canvasView.adLogoView;
    logoView.frame = CGRectMake(16, h, 45, 15);
    [adView addSubview:logoView];
    
    [self.view addSubview:adView];
    [ad registerContainer:adView withClickableViews:@[adView]];
}

- (void)nativeAdsManager:(BUNativeAdsManager *)adsManager didFailWithError:(NSError *_Nullable)error{
    
}

#pragma mark - BUNativeAdDelegate
/**
 This method is called when native ad material loaded successfully. This method will be deprecated. Use nativeAdDidLoad:view: instead
// Mediation:@Note :  Mediation dimension does not support this interface.
 */
- (void)nativeAdDidLoad:(BUNativeAd *)nativeAd{
    
}


/**
 This method is called when native ad material loaded successfully.
// Mediation:@Note :  Mediation dimension does not support this interface.
 */
- (void)nativeAdDidLoad:(BUNativeAd *)nativeAd view:(UIView *_Nullable)view{
    if (nativeAd.mediation.isExpressAd && view) {
        CGFloat h = view.frame.size.height;
        view.frame = CGRectMake(0, 100, kCXHScreenWidth, h);
        [self.view addSubview:view];
    }
}

/**
 This method is called when native ad materia failed to load.
 @param error : the reason of error
// Mediation:@Note :  Mediation dimension does not support this interface.
 */
- (void)nativeAd:(BUNativeAd *)nativeAd didFailWithError:(NSError *_Nullable)error{
    
}

/**
 This method is called when native ad slot has been shown.
 */
- (void)nativeAdDidBecomeVisible:(BUNativeAd *)nativeAd{
    
}

/**
 This method is called when another controller has been closed.
 @param interactionType : open appstore in app or open the webpage or view video ad details page.
 */
- (void)nativeAdDidCloseOtherController:(BUNativeAd *)nativeAd interactionType:(BUInteractionType)interactionType{
    
}

/**
 This method is called when native ad is clicked.
 */
- (void)nativeAdDidClick:(BUNativeAd *)nativeAd withView:(UIView *_Nullable)view{
    
}

/**
 This method is called when the user clicked dislike reasons.
 Only used for dislikeButton in BUNativeAdRelatedView.h
 @param filterWords : reasons for dislike
 */
- (void)nativeAd:(BUNativeAd *_Nullable)nativeAd dislikeWithReason:(NSArray<BUDislikeWords *> *_Nullable)filterWords{
    
}

/**
 This method is called when the Ad view container is forced to be removed.
 @param nativeAd : Ad material
 @param adContainerView : Ad view container
 */
- (void)nativeAd:(BUNativeAd *_Nullable)nativeAd adContainerViewDidRemoved:(UIView *)adContainerView{
    
}

/// This method will call when special ad event raised;
/// @param nativeAd ad
/// @param code eventCode
/// @param info event Info
- (void)nativeAd:(BUNativeAd *_Nullable)nativeAd onEventCode:(BUNativeAdEventCode)code info:(NSDictionary *)info{
    
}

#pragma mark -
/**
* Sent when views successfully load ad
*/
- (void)nativeExpressAdSuccessToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager views:(NSArray<__kindof BUNativeExpressAdView *> *)views{
    
}

/**
* Sent when views fail to load ad
*/
- (void)nativeExpressAdFailToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager error:(NSError *_Nullable)error{
    
}

/**
* This method is called when rendering a nativeExpressAdView successed, and nativeExpressAdView.size.height has been updated
*/
- (void)nativeExpressAdViewRenderSuccess:(BUNativeExpressAdView *)nativeExpressAdView{
    
}

/**
* This method is called when a nativeExpressAdView failed to render
*/
- (void)nativeExpressAdViewRenderFail:(BUNativeExpressAdView *)nativeExpressAdView error:(NSError *_Nullable)error{
    
}

/**
* Sent when an ad view is about to present modal content
*/
- (void)nativeExpressAdViewWillShow:(BUNativeExpressAdView *)nativeExpressAdView{
    
}

/**
* Sent when an ad view is clicked
*/
- (void)nativeExpressAdViewDidClick:(BUNativeExpressAdView *)nativeExpressAdView{
    
}

@end
